class Solution(object):
    def getPermutation(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: str
        """
        chooses = []
        def getTotalN(n):
            tot = 1
            for i in range(1, n+1):
                tot *= i
            return tot
        for i in range(n, 1, -1):
            tot = getTotalN(i-1)
            chooses.append((k-1)//tot)
            k %= tot
        chooses.append(0)

        candidate = [i for i in range(1, n+1)]
        for i in range(n):
            choice = chooses[i]
            chooses[i] = str(candidate[choice])
            candidate.pop(choice)
        return "".join(chooses)

for i in range(1, 7):
    print(Solution().getPermutation(3, i))